﻿using SSDM.CreateTableSchemaFactory;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CoreEA.SchemaInfo;
using System.Collections.ObjectModel;
using SSDM.GlobalDefine;
using CoreEA.LoginInfo;
using System;
using SSCEViewer_UnitTest;
namespace SqlCeViewer_UnitTest
{


    /// <summary>
    ///This is a test class for CreateTableSchema_SSCETest and is intended
    ///to contain all CreateTableSchema_SSCETest Unit Tests
    ///</summary>
    [TestClass()]
    public class CreateTableSchema_SSCETest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        ///// <summary>
        /////A test for InitializeComponent
        /////</summary>
        //[TestMethod()]
        //public void InitializeComponentTest()
        //{
        //    CreateTableSchema_SSCE target = new CreateTableSchema_SSCE(); // TODO: Initialize to an appropriate value
        //    target.InitializeComponent();
        //    target.Show();
        //}

        ///// <summary>
        /////A test for CreateTableSchema_SSCE Constructor
        /////</summary>
        //[TestMethod()]
        //public void CreateTableSchema_SSCEConstructorTest()
        //{
        //    CreateTableSchema_SSCE target = new CreateTableSchema_SSCE();
        //    Assert.Inconclusive("TODO: Implement code to verify target");
        //}


        ObservableCollection<CreateTableBindingArgs_SqlCe> bindingArgsList =
            new ObservableCollection<CreateTableBindingArgs_SqlCe>();

        [TestMethod()]
        public void TestCreateTableViaBaseSchemaInfo()
        {
            testContextInstance.RecordTestMethod();

            //Add test object
            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "ID",
                ColumnType = System.Data.SqlDbType.Int,
                IsPK = true,
                IsUnique = true,
            });

            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "CustomerID",
                ColumnType = System.Data.SqlDbType.Int,
                IsPK = true,
                IsUnique = true,
            });

            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "T1",
                ColumnType = System.Data.SqlDbType.Bit,
            });
            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "T2",
                ColumnType = System.Data.SqlDbType.Char,
                ColumnLength = 5,
            });

            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "T3",
                ColumnType = System.Data.SqlDbType.TinyInt,
            });

            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "Name",
                ColumnType = System.Data.SqlDbType.NVarChar,
                ColumnLength = 500,
            });

            bindingArgsList.Add(new CreateTableBindingArgs_SqlCe()
            {
                ColumnName = "Comment",
                ColumnType = System.Data.SqlDbType.DateTime,
                ColumnLength = 8,
            });

            //Generate table base schema info
            BaseTableSchema schemaInfo = new BaseTableSchema();

            string t = new Random().Next(1000).ToString();

            string tableName = string.Format("MyTestDataTable{0}", t);
            schemaInfo.TableName = tableName;

            foreach (var item in bindingArgsList)
            {
                if (item.IsPK)
                {
                    schemaInfo.PrimaryKey.Add(new BasePrimaryKeyInfo()
                    {
                        ColumnName = item.ColumnName,
                    });
                }

                schemaInfo.Columns.Add(new BaseColumnSchema()
                {
                    ColumnName = item.ColumnName,
                    ColumnType = item.ColumnType.ToString(),

                    IsNullable = item.AllowNulls,
                    CharacterMaxLength = item.ColumnLength,
                    IsIdentity = item.IsUnique,
                });
            }
            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlCE35).X_Handler;
            core.Open(new LoginInfo_SSCE() { DbName = "c:\\TestData\\EmptyCE35.sdf" });

            bool result = core.CreateTable(schemaInfo);

            if (result)
            {
                if (core.GetTableListInDatabase().Contains(schemaInfo.TableName))
                {
                    Assert.IsTrue(true);
                }
            }
            else
            {
                Assert.IsTrue(false);
            }
        }
    }
}
